package guiElements;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;

import tryout.NeuesEreignisFenster;

import myXMLPackage.XMLWorker;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;

public class Geburtstagserinnerung extends JFrame {

	private static final long serialVersionUID = 1L;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Geburtstagserinnerung thisClass = new Geburtstagserinnerung();
				thisClass.setDefaultCloseOperation(Geburtstagserinnerung.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}
	private JPanel jContentPane = null;
	private JPanel pnlConfig = null;
	private JLabel lblConfig = null;
	private JPanel pnlAktuell = null;
	private JLabel lblAktuell = null;
	private JPanel pnlAlle = null;
	private JLabel lblAlle = null;
	private JPanel pnlSubConfig = null;
	private JButton btnAdd = null;
	private JButton btnSearch = null;
	private NeuesEreignis nEreignis = null;
	private JButton jButton = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private XMLWorker worker = null;  //  @jve:decl-index=0:
	private DefaultTableModel tModel = null;
	private Geburtstagserinnerung me = null;
	private JPanel jPanel = null;
	private JCheckBox jCheckBox = null;
	private JCheckBox jCheckBox1 = null;
	private JLabel jLabel = null;

	/**
	 * This is the default constructor
	 */
	public Geburtstagserinnerung() {
		super();
		this.me = this;
		initialize();
	}

	protected void filterTable() {
		if(jCheckBox.isSelected()) {
			if(jCheckBox1.isSelected()) {
				updateTable(new DefaultTableModel(worker.getEvents(XMLWorker.ALLE), worker.getTableTitle()));
			} else {
				updateTable(new DefaultTableModel(worker.getEvents(XMLWorker.GEBURTSTAG), worker.getTableTitle()));
			}
		} else if(jCheckBox1.isSelected()) {
			updateTable(new DefaultTableModel(worker.getEvents(XMLWorker.TERMIN), worker.getTableTitle()));
		} else{
			updateTable(new DefaultTableModel(worker.getEvents(XMLWorker.KEINE), worker.getTableTitle()));
		}
	}

	/**
	 * This method initializes btnAdd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton();
			btnAdd.setBounds(new Rectangle(6, 7, 140, 20));
			btnAdd.setText("Neues Ereignis...");
			btnAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					nEreignis = new NeuesEreignis(worker, me);
					int x = (int) getPositionX();
					int y = (int) getPositionY();
					int w = (int) getW();
					int h = (int) getH();
					nEreignis.setBounds(x+w/2-nEreignis.getWidth()/2, y+h/2-nEreignis.getHeight()/2, nEreignis.getWidth(), nEreignis.getHeight());
				}
			});
		}
		return btnAdd;
	}

	/**
	 * This method initializes btnSearch	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton();
			btnSearch.setBounds(new Rectangle(160, 7, 140, 20));
			btnSearch.setText("Ereignis suchen...");
		}
		return btnSearch;
	}

	protected double getH() {
		return this.getBounds().getHeight();
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(6, 36, 173, 20));
			jButton.setText("Aktualisiere Terminliste");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// TODO : Die Tabelle muss aktualisiert werden
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setText("Geburtstage");
			jCheckBox.setSelected(true);
			jCheckBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					filterTable();
				}
			});
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jCheckBox1	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox1() {
		if (jCheckBox1 == null) {
			jCheckBox1 = new JCheckBox();
			jCheckBox1.setSelected(true);
			jCheckBox1.setText("Termine");
			jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					filterTable();
				}
			});
		}
		return jCheckBox1;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPnlConfig(), null);
			jContentPane.add(getPnlAktuell(), null);
			jContentPane.add(getPnlAlle(), null);
		}
		return jContentPane;
	}

	private JPanel getJPanel() {
		if(jPanel==null) {
			jLabel = new JLabel();
			jLabel.setText("Filter: ");
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
			flowLayout.setVgap(0);
			flowLayout.setHgap(5);
			jPanel = new JPanel();
			jPanel.add(jLabel, null);
			jPanel.add(getJCheckBox(), null);
			jPanel.add(getJCheckBox1(), null);
			jPanel.setLayout(flowLayout);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			worker = new XMLWorker();
			worker.readXML("XML/Termindaten2.xml");
			tModel = new DefaultTableModel(worker.getEvents(XMLWorker.ALLE), worker.getTableTitle());
			jTable = new JTable(tModel);
		}
		return jTable;
	}

	/**
	 * This method initializes pnlAktuell	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlAktuell() {
		if (pnlAktuell == null) {
			lblAktuell = new JLabel();
			lblAktuell.setText("Aktuelle Meldungen");
			lblAktuell.setFont(new Font("Dialog", Font.BOLD, 18));
			pnlAktuell = new JPanel();
			pnlAktuell.setLayout(new BorderLayout());
			pnlAktuell.setBounds(new Rectangle(506, 15, 257, 525));
			pnlAktuell.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			pnlAktuell.add(lblAktuell, BorderLayout.NORTH);
		}
		return pnlAktuell;
	}

	/**
	 * This method initializes pnlAlle	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlAlle() {
		if (pnlAlle == null) {
			lblAlle = new JLabel();
			lblAlle.setText("Alle Termine");
			lblAlle.setName("lblAlle");
			lblAlle.setFont(new Font("Dialog", Font.BOLD, 18));
			pnlAlle = new JPanel();
			pnlAlle.setLayout(new BorderLayout());
			pnlAlle.setBounds(new Rectangle(16, 15, 480, 423));
			pnlAlle.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			pnlAlle.add(lblAlle, BorderLayout.NORTH);
			pnlAlle.add(getJScrollPane(), BorderLayout.CENTER);
			pnlAlle.add(getJPanel(), BorderLayout.SOUTH);
		}
		return pnlAlle;
	}

	/**
	 * This method initializes pnlConfig	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlConfig() {
		if (pnlConfig == null) {
			lblConfig = new JLabel();
			lblConfig.setText("Konfiguration");
			lblConfig.setFont(new Font("Dialog", Font.BOLD, 18));
			pnlConfig = new JPanel();
			pnlConfig.setLayout(new BorderLayout());
			pnlConfig.setBounds(new Rectangle(16, 448, 480, 93));
			pnlConfig.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			pnlConfig.add(lblConfig, BorderLayout.NORTH);
			pnlConfig.add(getPnlSubConfig(), BorderLayout.CENTER);
		}
		return pnlConfig;
	}

	/**
	 * This method initializes pnlSubConfig	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlSubConfig() {
		if (pnlSubConfig == null) {
			pnlSubConfig = new JPanel();
			pnlSubConfig.setLayout(null);
			pnlSubConfig.add(getBtnAdd(), null);
			pnlSubConfig.add(getBtnSearch(), null);
			pnlSubConfig.add(getJButton(), null);
		}
		return pnlSubConfig;
	}

	protected double getPositionX() {
		return this.getBounds().getX();
	}

	protected double getPositionY() {
		return this.getBounds().getY();
	}

	protected double getW() {
		return this.getBounds().getWidth();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(800, 600);
		this.setContentPane(getJContentPane());
		this.setTitle("Geburtstagserinnerung 2.0");
	}

	public void updateTable(DefaultTableModel dTM) {
		dTM.removeRow(0);
		
		jTable.setModel(dTM);
	}

}
